﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;

namespace OnenetDataPushNdPop
{
    class OnenetPostData
    {
        public static void SetHeaderValue(WebHeaderCollection header, string name, string value)// HTTP协议报文头加入

        {
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(header, null) as NameValueCollection;
                collection[name] = value;
            }
        }
        //HTML API Get data
        private string Get()//连接服务器并获取数据
        {
            string url = "http://api.heclouds.com/devices/628551504/datapoints?";//设备地址
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            SetHeaderValue(request.Headers, "api-key", "5N5VzjGqkLu3TXdGhpJcvO5SdHE=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }
        //HTML API Post data
        public string Post()
        {
            string sendData;
            string url = "http://api.heclouds.com/devices/628551504/datapoints?";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "5N5VzjGqkLu3TXdGhpJcvO5SdHE=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{\"datastreams\":[{\"id\":\"temperature\",\"datapoints\":[{\"value\":\"" + "1234" + "\"}]},{\"id\":\"RH\",\"datapoints\":[{\"value\":\"" + "1234" + "\"}]},{\"id\":\"state\",\"datapoints\":[{\"value\":\"" + "5678" + "\"}]}]}";
            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                sendData = reader.ReadToEnd();
                //pictureBox1.BackColor = Color.Lime;
            }
            return sendData;
        }
        //HTML API Register device
        public void RegDev()
        {
            string url = "http://api.heclouds.com/register_de";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "4oJhh6NdlNJRwxrWulCDjfGazDw=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{\"title\":\"mqtt_device\",\"desc\":\"test mqtt\",\"private\":true,\"protocol\":\"MQTT\",\"auth_info\":\"HuangQilong\",}";
            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {

                //pictureBox1.BackColor = Color.Lime;
            }
        }
        //HTML API Create data flow
        public void createDataFlow()
        {
            string url = "http://api.heclouds.com/devices/628955397/datastreams";
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            request.Method = "POST";
            SetHeaderValue(request.Headers, "api-key", "4oJhh6NdlNJRwxrWulCDjfGazDw=");//设备API地址和 首部参数
            request.Host = "api.heclouds.com";
            request.ProtocolVersion = new Version(1, 1);
            string Cod = "{\"id\":\"DiagHostFlow\",\"desc\":\"Diagnosis data flow send by pc\",}";
            byte[] data = Encoding.UTF8.GetBytes(Cod);
            request.ContentLength = data.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容 
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
            }
        }
    }
}
